﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;

public partial class khdj : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }

        CreateDDItem();

        SetDefault();

    }

    protected void CreateDDItem()
    {
        util.CreateDownList(this.lytj,"laiyuan");
        util.CreateDownList(this.yxqd, "yixiang");
        util.CreateDownList(this.fkfs, "fukuan");
        util.CreateDownListWithQuanBu(this.hx, "huxing");
        util.CreateDownListWithQuanBu(this.lh, "louhao");
        util.CreateDownList(this.quyu, "quyu");
        util.CreateDownListSpVal(this.xb, "男", "1");
        util.CreateDownListSpVal(this.xb, "女", "0");
        util.CreateDownList(this.ywdb, "yuangong");        
    }

    protected void SetDefault()
    {
        NameValueCollection m_args = Request.QueryString;

        if (m_args != null)
        {
            string strxm = m_args["xm"].ToString();
            string strdh = m_args["dh"].ToString();
            if (strxm == "" && strdh=="")
            {
                return;
            }
            string strsql = "select * from [fangwen] where ";
            if (strxm != "")
            {
                this.xm.Value = strxm;
                strsql += "[name] = '" + strxm + "'";
                if (strdh != "")
                {
                    strsql += " and ";
                }
            }

            if (strdh != "")
            {
                this.lxfs.Value = strdh;
                strsql += "[phone] = '" + strdh + "'";
            }

            ManageData md = new ManageData(util.DATACONNECTION);
            if (md != null)
            {
                DataSet ds = md.ExecDS(strsql);

                if (ds != null && ds.Tables.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {                    
                       this.xm.Value = dr["name"].ToString();
                       this.lxfs.Value = dr["phone"].ToString();
                       this.xb.SelectedValue = dr["sex"].ToString();
                       this.nl.Value = dr["age"].ToString();
                       this.quyu.SelectedValue = dr["quyu"].ToString();
                       this.lytj.SelectedValue = dr["laiyuan"].ToString();
                       this.lfrs.Value = dr["laifangrenshu"].ToString();
                       this.gzdw.Value = dr["danwei"].ToString();
                       this.ywdb.SelectedValue = dr["p_id"].ToString();
                       this.yxqd.SelectedValue = dr["yixiang"].ToString();
                       this.fkfs.SelectedValue = dr["fukuan"].ToString();
                       this.hx.SelectedValue = dr["huxing"].ToString();
                       this.mj.Value = dr["mianji"].ToString();
                       this.lc.Value = dr["loucheng"].ToString();
                       this.lh.SelectedValue = dr["louhao"].ToString();
                                                
                    }
                }  
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string strxm = this.xm.Value.Trim();
        string strlxfs = this.lxfs.Value.Trim();
        string strxb = this.xb.SelectedValue.Trim();
        string strnl = this.nl.Value.Trim();
        string strquyu = this.quyu.SelectedValue.Trim();
        string strlaiyuan = this.lytj.SelectedValue.Trim();
        string strlfrs = this.lfrs.Value.Trim();
        string strgzdw = this.gzdw.Value.Trim();
        string strxsdb = this.ywdb.SelectedValue;
        string stryxqd = this.yxqd.SelectedValue;
        string strfkfs = this.fkfs.SelectedValue;
        string strgxh = this.hx.SelectedValue;
        string strmianji = this.mj.Value.Trim();
        string strlc = this.lc.Value.Trim();
        string strlh = this.lh.SelectedValue;
        string strlfsj = this.lfsj.Value;
        string strbz = this.bz.Value.Trim();
        string strlftxt = this.lftxt.Value;
        string strType = "1";

        if (this.ldjl.Checked)
        {
            strType = "0";
            strlfsj = this.ldsj.Value;
            strlftxt = this.ldtxt.Value;
        }

        string strsql = string.Format(@"INSERT INTO[fangwen](
[name], 
[age],
[sex], 
[phone],
[danwei],
[quyu],
[laiyuan],
[yixiang],
[loucheng],
[huxing], 
[benzhu],
[louhao],
[p_id],
[laifangrenshu],
[fukuan],
[mianji],
[type],
[msg],
[dt]
)
VALUES(
'{0}',
'{1}',
'{2}',
'{3}',
'{4}',
'{5}',
'{6}',
'{7}',
'{8}',
'{9}',
'{10}',
'{11}',
'{12}',
'{13}',
'{14}',
'{15}',
'{16}',
'{17}',
'{18}'
);
",
 strxm,
 strnl,
 strxb,
 strlxfs,
 strgzdw,
 strquyu,
 strlaiyuan,
 stryxqd, 
 strlc, 
 strgxh,
 strbz,
 strlh,
 strxsdb,
 strlfrs, 
 strfkfs, 
 strmianji, 
 strType, 
 strlftxt,
 strlfsj);

        ManageData md = new ManageData(util.DATACONNECTION);
        if (md != null)
        {
            int n = md.ExecNonQuery(strsql);
            if (n >0)
            {
                Response.Write(util.GetResponseAlert("登记成功"));
            }
            else
            {
                Response.Write(util.GetResponseAlert("登记失败"));
            }
        }



    }
}
